import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pornhub_app/assets/styles.dart';
import 'package:pornhub_app/components/app_bar/app_bar_view.dart';
import 'package:pornhub_app/components/base_page/base_loading_widget.dart';
import 'package:pornhub_app/components/easy_toast.dart';
import 'package:pornhub_app/components/image_view.dart';
import 'package:pornhub_app/components/popups/buy_fail_dialog.dart';
import 'package:pornhub_app/components/text_view.dart';
import 'package:pornhub_app/generate/app_image_path.dart';
import 'package:pornhub_app/services/user_service.dart';
import 'package:pornhub_app/utils/color.dart';
import 'package:pornhub_app/utils/extension.dart';
import 'package:pornhub_app/views/activity/controllers/activity_page_controller.dart';

import '../../../routes/routes.dart';

class ActivityDetailPage extends StatefulWidget {
  const ActivityDetailPage({super.key});

  @override
  State<ActivityDetailPage> createState() => _ActivityPageState();
}

class _ActivityPageState extends State<ActivityDetailPage> {
  int activityId = Get.arguments["activityId"] ?? 0;

  PreferredSizeWidget _buildAppBar() => AppBarView(
        titleText: '活动详情',
      );

  late ActivityPageController controller;

  @override
  void initState() {
    controller = Get.put(ActivityPageController(), tag: "${activityId}");
    controller.getActivityDetailById(activityId);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: COLOR.color_111,
      appBar: _buildAppBar(),
      body: GetBuilder<ActivityPageController>(
          tag: "${activityId}",
          builder: (_) {
            return !controller.isLoadData
                ? BaseLoadingWidget()
                : CustomScrollView(
                    slivers: [
                      Stack(
                        children: [
                          Row(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              ImageView(
                                src: "${controller.detailModel.coverImg}",
                                width: 120.w,
                                height: 120.w,
                                borderRadius: BorderRadius.circular(8.w),
                              ),
                              Expanded(
                                  child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  TextView(
                                    text:
                                        "${controller.detailModel.activityName}",
                                    style:
                                        kTextStyle(COLOR.white, fontsize: 14.w),
                                  ).paddingOnly(top: 8.w),
                                  if (controller.detailModel.tags != null)
                                    Container(
                                      height: 20.w,
                                      width: 372.w,
                                      margin: EdgeInsets.only(top: 14.w),
                                      child: ListView.builder(
                                          itemCount: controller
                                              .detailModel.tags?.length,
                                          scrollDirection: Axis.horizontal,
                                          shrinkWrap: true,
                                          itemBuilder: (ctx, index) {
                                            return Container(
                                              alignment: Alignment.center,
                                              margin:
                                                  EdgeInsets.only(right: 8.w),
                                              padding: EdgeInsets.only(
                                                  left: 8.w, right: 8.w),
                                              decoration: BoxDecoration(
                                                  color: COLOR.color_333333,
                                                  borderRadius:
                                                      BorderRadius.circular(
                                                          10.w)),
                                              child: TextView(
                                                text:
                                                    "${controller.detailModel.tags![index]}",
                                                style: kTextStyle(COLOR.white,
                                                    fontsize: 11.w),
                                              ),
                                            );
                                          }),
                                    )
                                ],
                              ).paddingOnly(left: 8.w, top: 8.w)),
                            ],
                          ),
                          if (controller.detailModel.alreadyJoin == true)
                            Positioned(
                                right: 0,
                                bottom: 0,
                                child: ImageView(
                                  src: AppImagePath.activity_activity_join_1,
                                  width: 58.2.w,
                                  height: 51.w,
                                )),
                          if (controller.detailModel.isReview == true)
                            Positioned(
                                right: 0,
                                bottom: 0,
                                child: ImageView(
                                  src: AppImagePath.activity_activity_shenhe_1,
                                  width: 58.2.w,
                                  height: 51.w,
                                ))
                        ],
                      ).marginHorizontal(14.w).sliver,
                      Container(
                        width: 372.w,
                        padding:
                            EdgeInsets.only(left: 14.w, top: 15.w, bottom: 8.w),
                        decoration: BoxDecoration(
                            color: COLOR.color_181818,
                            borderRadius: BorderRadius.circular(4.w)),
                        child: Column(
                          children: [
                            Row(
                              children: [
                                ImageView(
                                  src: AppImagePath.activity_activity_time,
                                  width: 14.w,
                                  height: 14.w,
                                ),
                                TextView(
                                  text: "时间",
                                  style: kTextStyle(
                                      const Color.fromRGBO(255, 255, 255, 0.54),
                                      fontsize: 12.w),
                                ).paddingLeft(8.w),
                                TextView(
                                  text:
                                      "${controller.detailModel.activityDate}",
                                  style:
                                      kTextStyle(COLOR.white, fontsize: 12.w),
                                ).paddingLeft(10.w)
                              ],
                            ),
                            Row(
                              children: [
                                ImageView(
                                  src: AppImagePath.activity_activity_location,
                                  width: 12.w,
                                  height: 15.w,
                                ),
                                TextView(
                                  text: "地址",
                                  style: kTextStyle(
                                      const Color.fromRGBO(255, 255, 255, 0.54),
                                      fontsize: 12.w),
                                ).paddingLeft(8.w),
                                Expanded(
                                    child: Text(
                                  "${controller.detailModel.locationDetail}",
                                  style:
                                      kTextStyle(COLOR.white, fontsize: 12.w),
                                ).paddingLeft(10.w))
                              ],
                            ).marginOnly(top: 15.w, bottom: 4.w),
                          ],
                        ),
                      ).marginOnly(top: 14.w, left: 14.w, right: 14.w).sliver,
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          const TextView(text: "活动发起人").marginBottom(10.w),
                          Container(
                              height: 80.w,
                              width: 372.w,
                              padding: EdgeInsets.only(
                                  left: 14.w, top: 15.w, right: 14.w),
                              decoration: BoxDecoration(
                                  color: COLOR.color_181818,
                                  borderRadius: BorderRadius.circular(4.w)),
                              child: Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  Row(
                                    children: [
                                      ImageView(
                                        src:
                                            "${controller.detailModel.organizerProfilePicture}",
                                        width: 60.w,
                                        height: 60.w,
                                        borderRadius:
                                            BorderRadius.circular(8.w),
                                      ),
                                      TextView(
                                        text:
                                            "${controller.detailModel.organizerName}",
                                        style: kTextStyle(Colors.white,
                                            fontsize: 14.w,
                                            weight: FontWeight.w600),
                                      ).paddingLeft(8.w),
                                    ],
                                  ),
                                  Container(
                                    width: 80.w,
                                    height: 27.w,
                                    alignment: Alignment.center,
                                    decoration: BoxDecoration(
                                        borderRadius:
                                            BorderRadius.circular(13.5.w),
                                        border: Border.all(
                                            color: COLOR.color_B940FF)),
                                    child: TextView(
                                      text: '私信发起人',
                                      style: TextStyle(
                                          fontSize: 12.w,
                                          color: COLOR.color_B940FF),
                                    ),
                                  ).onOpaqueTap(() {
                                    // Get.toChat(
                                    //     userId: int.parse(controller
                                    //             .detailModel.organizerId ??
                                    //         "0"),
                                    //     userNickName: controller
                                    //             .detailModel.organizerName ??
                                    //         "");
                                  })
                                ],
                              )),
                        ],
                      ).marginOnly(top: 20.w, left: 14.w, right: 14.w).sliver,
                      if (controller.detailModel.currentParticipants != 0)
                        Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            TextView(
                                    text:
                                        "已报名 ${controller.detailModel.currentParticipants}/${controller.detailModel.participantLimit}")
                                .marginBottom(10.w),
                            Container(
                                height: 85.w,
                                width: 372.w,
                                alignment: Alignment.centerLeft,
                                padding:
                                    EdgeInsets.only(left: 14.w, right: 14.w),
                                decoration: BoxDecoration(
                                    color: COLOR.color_181818,
                                    borderRadius: BorderRadius.circular(4.w)),
                                child: Row(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceBetween,
                                  children: [
                                    Expanded(
                                        child: ListView.builder(
                                            scrollDirection: Axis.horizontal,
                                            itemCount: controller.detailModel
                                                .participants?.length,
                                            itemBuilder: (context, index) {
                                              return Column(
                                                mainAxisAlignment:
                                                    MainAxisAlignment.center,
                                                children: [
                                                  ImageView(
                                                    src: controller
                                                            .detailModel
                                                            .participants?[
                                                                index]
                                                            .logo ??
                                                        "",
                                                    width: 40.w,
                                                    height: 40.w,
                                                    borderRadius:
                                                        BorderRadius.circular(
                                                            40.w),
                                                  ),
                                                  Container(
                                                    width: 56.w,
                                                    alignment: Alignment.center,
                                                    padding: EdgeInsets.only(
                                                        top: 6.w),
                                                    child: TextView(
                                                      text:
                                                          "${controller.detailModel.participants?[index].nickName}",
                                                      textAlign:
                                                          TextAlign.center,
                                                      maxLines: 1,
                                                      overflow:
                                                          TextOverflow.ellipsis,
                                                      style: kTextStyle(
                                                          COLOR.color_A4A4B2,
                                                          fontsize: 11.w,
                                                          weight:
                                                              FontWeight.w400),
                                                    ),
                                                  ),
                                                ],
                                              ).marginRight(4.w);
                                            })),
                                    Container(
                                      width: 32.w,
                                      height: 56.w,
                                      alignment: Alignment.center,
                                      decoration: BoxDecoration(
                                        color: COLOR.color_323232,
                                        borderRadius:
                                            BorderRadius.circular(4.w),
                                      ),
                                      child: Column(
                                        mainAxisAlignment:
                                            MainAxisAlignment.center,
                                        children: [
                                          TextView(
                                            text: '查看\n全部',
                                            style: TextStyle(
                                                fontSize: 12.w,
                                                color: COLOR.color_A4A4B2),
                                          ),
                                          const Icon(
                                            Icons.arrow_forward_ios,
                                            color: COLOR.color_B93FFF,
                                            size: 12,
                                          )
                                        ],
                                      ),
                                    ).onOpaqueTap(() {
                                      Get.toNamed(Routes.activityPlayer,
                                          arguments: {
                                            "activityId": activityId
                                          });
                                    })
                                  ],
                                )),
                          ],
                        ).marginOnly(top: 20.w, left: 14.w, right: 14.w).sliver,
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          const TextView(text: "活动图片").marginBottom(10.w),
                          Container(
                              height: 146.w,
                              width: 372.w,
                              alignment: Alignment.centerLeft,
                              child: ListView.builder(
                                  scrollDirection: Axis.horizontal,
                                  itemCount: controller
                                      .detailModel.detailImages?.length,
                                  padding: EdgeInsets.zero,
                                  itemBuilder: (context, index) {
                                    return ImageView(
                                      src: controller.detailModel
                                              .detailImages?[index] ??
                                          "",
                                      width: 112.w,
                                      height: 146.w,
                                      borderRadius: BorderRadius.circular(8.w),
                                    ).marginRight(8.w);
                                  })),
                        ],
                      ).marginOnly(top: 20.w, left: 14.w, right: 14.w).sliver,
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          const TextView(text: "活动详情").marginBottom(10.w),
                          Container(
                              width: 372.w,
                              alignment: Alignment.centerLeft,
                              padding: EdgeInsets.all(14.w),
                              decoration: BoxDecoration(
                                  color: COLOR.color_181818,
                                  borderRadius: BorderRadius.circular(4.w)),
                              child: TextView(
                                  style:
                                      kTextStyle(COLOR.white, fontsize: 12.w),
                                  text:
                                      "${controller.detailModel.description}")),
                        ],
                      ).marginOnly(top: 20.w, left: 14.w, right: 14.w).sliver,
                      // Column(
                      //   crossAxisAlignment: CrossAxisAlignment.start,
                      //   children: [
                      //     const TextView(text: "发起人评价").marginBottom(10.w),
                      //     ListView.builder(
                      //         padding: EdgeInsets.zero,
                      //         itemCount: 5,
                      //         shrinkWrap: true,
                      //         physics: const NeverScrollableScrollPhysics(),
                      //         itemBuilder: (context, index) {
                      //           return Container(
                      //             margin: EdgeInsets.only(bottom: 20.w),
                      //             child: Row(
                      //               crossAxisAlignment:
                      //                   CrossAxisAlignment.start,
                      //               children: [
                      //                 ImageView(
                      //                   src: AppImagePath.icon_arround_bg,
                      //                   width: 35.w,
                      //                   height: 35.w,
                      //                   borderRadius:
                      //                       BorderRadius.circular(35.w),
                      //                 ),
                      //                 Column(
                      //                   crossAxisAlignment:
                      //                       CrossAxisAlignment.start,
                      //                   children: [
                      //                     TextView(
                      //                       text: "你是我的神",
                      //                       style: kTextStyle(
                      //                           COLOR.color_9C9AA9,
                      //                           fontsize: 12.w),
                      //                     ),
                      //                     Container(
                      //                       width: 322.w,
                      //                       margin: EdgeInsets.only(top: 4.w),
                      //                       alignment: Alignment.centerLeft,
                      //                       child: Text(
                      //                           "气氛超好，发起人很帖子，给人一种很亲切的感觉，本来还担心是不是会有些尴尬，但是发起人很会活跃气氛，大家都很融洽。很开心的聚会",
                      //                           softWrap: true,
                      //                           style: kTextStyle(
                      //                             COLOR.hexColor("#FEFFFE"),
                      //                             fontsize: 12.w,
                      //                           )),
                      //                     )
                      //                   ],
                      //                 ).marginLeft(9.w)
                      //               ],
                      //             ),
                      //           );
                      //         }),
                      //   ],
                      // ).marginOnly(top: 20.w, left: 14.w, right: 14.w).sliver,
                      if (controller.detailModel.status == 0 &&
                          controller.detailModel.alreadyJoin == false &&
                          controller.detailModel.isReview == false)
                        ImageView(
                          src: AppImagePath.activity_activity_notjoin_btn,
                          width: 360.w,
                          height: 41.w,
                        )
                            .marginOnly(left: 14.w, right: 14.w, top: 20.w)
                            .onOpaqueTap(() {
                          showJoinDialog();
                          // controller.detailModel.
                        }).sliver,
                      if (controller.detailModel.alreadyJoin == true)
                        ImageView(
                          src: AppImagePath.activity_activity_join_2,
                          width: 360.w,
                          height: 41.w,
                        ).marginOnly(left: 14.w, right: 14.w, top: 20.w).sliver,
                      if (controller.detailModel.isReview == true)
                        ImageView(
                          src: AppImagePath.activity_activity_shenhe_2,
                          width: 360.w,
                          height: 41.w,
                        ).marginOnly(left: 14.w, right: 14.w, top: 20.w).sliver,
                      Container(
                        height: 44.w,
                      ).sliver,
                    ],
                  );
          }),
    );
  }

  showJoinDialog() {
    Get.dialog(Center(
        child: Container(
      width: 300.w,
      height: 212.w,
      decoration: BoxDecoration(
          color: COLOR.white, borderRadius: BorderRadius.circular(12.w)),
      child: Column(
        children: [
          Container(
            height: 52.w,
            alignment: Alignment.center,
            child: TextView(
              text: "申请报名",
              style: kTextStyle(COLOR.color_333333,
                  fontsize: 16.w, weight: FontWeight.w600),
            ),
          ),
          Container(
            width: 300.w,
            height: 1.w,
            color: COLOR.color_F0F0F0,
          ),
          Container(
            margin: EdgeInsets.only(top: 13.w),
            padding: EdgeInsets.only(left: 13.w, right: 13.w),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                TextView(
                    textAlign: TextAlign.left,
                    style: kTextStyle(COLOR.color_333333, fontsize: 12.w),
                    text: "1.报名后请耐心等待活动发起人审核，审核通过后即为报名活动成功，同时加入活动临时群聊"),
                TextView(
                    textAlign: TextAlign.center,
                    style: kTextStyle(COLOR.color_333333, fontsize: 12.w),
                    text: "2.请诚信守约按时参加活动"),
                TextView(
                    textAlign: TextAlign.center,
                    style: kTextStyle(COLOR.color_333333, fontsize: 12.w),
                    text: "3.请务必注意自身安全，谨防上当受骗")
              ],
            ),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              ImageView(
                src: AppImagePath.activity_activity_confirm_btn,
                width: 172.w,
                height: 36.w,
                borderRadius: BorderRadius.circular(36.w),
              ).onOpaqueTap(() async {
                if (controller.detailModel.price! > 0) {
                  final ass = Get.find<UserService>().assets.gold ?? 0;
                  if (ass == 0) {
                    Get.back();
                    await BuyFailDialog().show();
                  }
                  final p = controller.detailModel.price!;
                  if (ass < p) {
                    Get.back();
                    await BuyFailDialog().show();
                  } else {
                    var r = await controller.joinByActivityId(activityId);
                    if (r == true) {
                      EasyToast.show("报名成功,等待审核");
                      Get.back();
                    }
                  }
                } else {
                  var r = await controller.joinByActivityId(activityId);
                  if (r == true) {
                    EasyToast.show("报名成功,等待审核");
                    Get.back();
                  }
                }
              })
            ],
          ).marginOnly(top: 20.w, left: 64.w, right: 64.w, bottom: 16.w)
        ],
      ),
    )));
  }
}
